package test06;

import java.util.*;

public class MyArrayList implements List {
    private int maxSize=100;
    private Object objs[]=new Object[maxSize];
    private int size=0;

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        if(objs[0]!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean contains(Object o) {
        for(Object obj:objs){
            if(obj.equals(o)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        Object objs1[]=new Object[size];
        for(int i=0;i<size;i++){
            objs1[i]=objs[i];
        }
        return objs1;
    }

    @Override
    public boolean add(Object o) {
        int oldSize=size;
        if(size<maxSize){
            objs[size]=o;
            size++;
            return true;
        }
        return false;
    }

    @Override
    public boolean remove(Object o) {
        for(int i=0;i<objs.length;i++){
            if(objs[i].equals(o)){
                for(int j=i;j<objs.length-1;j++){
                    objs[j]=objs[j+1];
                }
                size--;
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean addAll(Collection c) {
        return addAll(0,c);
    }

    @Override
    public boolean addAll(int index, Collection c) {
        int oldSize=size;
        MyArrayList list=(MyArrayList)c;
        for(int i=oldSize;i<list.size()+oldSize-index;i++){
            objs[i]=list.objs[i-oldSize+index];
            size++;
        }
        if(size==c.size()+oldSize){
            return true;
        }
        return false;
    }

    @Override
    public void clear() {
        int oldSize=size;
        for(int i=0;i<oldSize;i++){
            objs[i]=null;
            size--;
        }
    }

    @Override
    public Object get(int index) {
        if(index<size && index>=0){
            return objs[index];
        }
        return null;
    }

    @Override
    public Object set(int index, Object element) {
        if(index>=0 && index<size){
            objs[index]=element;
            return element;
        }else{
            return null;
        }
    }

    @Override
    public void add(int index, Object element) {
        for(int i=size-1;i>=index;i--){
            objs[i+1]=objs[i];
        }
        objs[index]=element;
        size++;
    }

    @Override
    public Object remove(int index) {
        if(index>=0 && index<size){
            Object object=objs[index];
            for(int i=index;i<size-1;i++){
                objs[i]=objs[i+1];
            }
            size--;
            return object;
        }else{
            return null;
        }
    }

    @Override
    public int indexOf(Object o) {
        for(int i=0;i<size;i++){
            if(objs[i]==o){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int lastIndexOf(Object o) {
        for(int i=size-1;i>=0;i--){
            if(objs[i]==o){
                return i;
            }
        }
        return -1;
    }

    @Override
    public ListIterator listIterator() {
        return null;
    }

    @Override
    public ListIterator listIterator(int index) {
        return null;
    }

    @Override
    public List subList(int fromIndex, int toIndex) {
        List list=new MyArrayList();
        for(int i=fromIndex;i<toIndex;i++){
            list.add(objs[i]);
        }
        return list;
    }

    @Override
    public boolean retainAll(Collection c) {
        MyArrayList list=(MyArrayList)c;
        for(int i=0;i<size;i++){
            if(list.indexOf(objs[i])==-1){
                this.remove(i);
                i--;
            }
        }
        return true;
    }

    @Override
    public boolean removeAll(Collection c) {
        MyArrayList list=(MyArrayList)c;
        for(int i=0;i<list.size;i++){
            for(int j=0;j<size;j++){
                if(objs[j]==list.objs[i]){
                    remove(j);
                }
            }
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection c) {
        MyArrayList list=(MyArrayList)c;
        int count=0;
        for(int i=0;i<list.size;i++){
            for(int j=0;j<size;j++){
                if(objs[j]==list.objs[i]){
                    count++;
                    break;
                }
            }
        }
        if(count==list.size){
            return true;
        }
        return false;
    }

    @Override
    public Object[] toArray(Object[] a) {
        int count=0;
        for(int i=0;i<size;i++){
            for(Object obj:a){
                if(objs[i].equals(obj)){
                    count++;
                }
            }
        }
        Object objs1[]=new Object[count];
        int j=0;
        for(int i=0;i<size;i++){
            for(Object obj:a){
                if(objs[i].equals(obj)){
                    objs1[j]=obj;
                    j++;
                }
            }
        }
        return objs1;
    }

    @Override
    public String toString(){
        String str="";
        if(size>1){
            for (int i = 0; i < size; i++) {
                if(i==0){
                    str+="["+objs[i]+",";
                }else if(i>0 && i<size-1){
                    str+=objs[i]+",";
                }else{
                    str+=objs[i]+"]";
                }
            }
        }else if(size==1){
            str="["+objs[0]+"]";
        }else{
            str="[]";

        }
        return str;
    }
}
